//
//  EmotionViewController.m
//  Games
//
//  Created by iOS Two on 14/12/26.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "EmotionViewController.h"
#import "UIImage+GIF.h"
#import "NSData+GIF.h"
#import <ShareSDK/ShareSDK.h>
#import "WeiBoViewController.h"
#import "UIImageView+MJWebCache.h"
#import "myData.h"
#import "myObj.h"

@interface EmotionViewController ()

@end

@implementation EmotionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
    label.text = _gifName;
    label.font = [UIFont fontWithName:@"MFYaYuan_Noncommercial-Regular" size:20];
    label.textColor = [UIColor colorWithRed:0.98 green:0.59 blue:0.16 alpha:1];
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = label;
    
    UIImageView *imageBack = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kwidth, kHeight)];
    imageBack.image = [UIImage imageNamed:@"di6_02@3x"];
    [self.view addSubview:imageBack];
    
    [self getData];
    // Do any additional setup after loading the view.
}
-(BOOL)findSQl {
    myData *data = [[myData alloc]init];
    [data initData];
    NSArray *array = [data selectData];
    for (int i = 0; i < array.count; i++) {
        myObj *obj = [array objectAtIndex:i];
        if ([_gifName isEqualToString:obj.gifName]) {
            return YES;
        }
    }
    return NO;
}
-(void)removeShare {
    if ([_share superview]) {
        [UIView animateWithDuration:0.5 animations:^{
            if (kwidth == 320) {
                if (kHeight == 480) {
                    _share.frame = CGRectMake(5, kHeight, kwidth - 10, 220);
                } else {
                    _share.frame = CGRectMake(5, kHeight, kwidth - 10, 220);
                }
            } else if (kwidth == 414) {
                _share.frame = CGRectMake(5, kHeight, kwidth - 10, 220);
            } else if (kwidth == 375) {
                _share.frame = CGRectMake(5, kHeight, kwidth - 10, 220);
            }
        } completion:^(BOOL finished) {
            [_back removeFromSuperview];
        }];
    }
}
-(void)getData {
    _gifArray = [[NSMutableArray alloc]init];
    _gifNameArray = [[NSMutableArray alloc]init];
    _gifUrlArray = [[NSMutableArray alloc]init];
    _gifDataArray = [[NSMutableArray alloc]init];
    
    if ([_gifName isEqualToString:@"蜡笔小新"]|[_gifName isEqualToString:@"Hello Kitty"]|[_gifName isEqualToString:@"甜甜私房猫"]) {
        _isPath = YES;
    } else {
        _isPath = NO;
    }
    _gifNum = 0;

    if ([_gifName isEqualToString:@"蜡笔小新"]) {
        _gifArray = [NSMutableArray arrayWithObjects:@"哆啦A梦—1",@"哆啦A梦—2",@"哆啦A梦—3",@"哆啦A梦—4",@"哆啦A梦—5",@"哆啦A梦—6",@"哆啦A梦—7",@"哆啦A梦—8",@"哆啦A梦—9",@"哆啦A梦—10",@"哆啦A梦—11",@"哆啦A梦—12",@"哆啦A梦—13",@"哆啦A梦—14",@"哆啦A梦—15",@"哆啦A梦—16", nil];
        [self getPath];
    } else if ([_gifName isEqualToString:@"Hello Kitty"]) {
        _gifArray = [NSMutableArray arrayWithObjects:@"hello kitty_1",@"hello kitty_2",@"hello kitty_3",@"hello kitty_4",@"hello kitty_5",@"hello kitty_6",@"hello kitty_7",@"hello kitty_8",@"hello kitty_9",@"hello kitty_10",@"hello kitty_11",@"hello kitty_12",@"hello kitty_13",@"hello kitty_14",@"hello kitty_15",@"hello kitty_16",@"hello kitty_17",@"hello kitty_18",@"hello kitty_19",@"hello kitty_20",@"hello kitty_21",@"hello kitty_22",@"hello kitty_23",@"hello kitty_24",nil];
        [self getPath];
    } else if ([_gifName isEqualToString:@"甜甜私房猫"]) {
        _gifArray = [NSMutableArray arrayWithObjects:@"甜甜私房猫-1",@"甜甜私房猫-2",@"甜甜私房猫-3",@"甜甜私房猫-4",@"甜甜私房猫-5",@"甜甜私房猫-6",@"甜甜私房猫-7",@"甜甜私房猫-8",@"甜甜私房猫-9",@"甜甜私房猫-10",@"甜甜私房猫-11",@"甜甜私房猫-12",@"甜甜私房猫-13",@"甜甜私房猫-14",@"甜甜私房猫-15",@"甜甜私房猫-16",@"甜甜私房猫-17",@"甜甜私房猫-18",@"甜甜私房猫-19",@"甜甜私房猫-20",@"甜甜私房猫-21",@"甜甜私房猫-22",@"甜甜私房猫-23",@"甜甜私房猫-24",nil];
        
        [self getPath];
    } else {
        [self showIndeterminateProgress];
        AVQuery *query = [[AVQuery alloc]initWithClassName:@"BiaoQing"];
        [query whereKey:@"biaoQingBao" equalTo:_gifName];
        query.cachePolicy = kPFCachePolicyCacheElseNetwork;
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (error == nil) {
                for (int i = 0; i < objects.count; i++) {
                    if (_hasName) {
                        [_gifNameArray addObject:[[objects objectAtIndex:i] objectForKey:@"gifName"]];
                    }
                    AVFile *file = [[objects objectAtIndex:i] objectForKey:@"gifImage"];
                    [_gifArray addObject:file.url];
                    [_gifDataArray addObject:[file getData]];
                }
                [HUD hide:YES];
                [self creatController];
            } else {
                [HUD hide:YES];
                [self alertViewShow:@"加载失败,请重试"];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
    }
}
-(void)getPath {
    for (int i = 0; i < _gifArray.count; i++) {
        NSString *path = [[NSBundle mainBundle] pathForResource:[_gifArray objectAtIndex:i] ofType:@"gif"];
        NSData *gifData = [NSData dataWithContentsOfFile:path];
        [_gifUrlArray addObject:path];
        [_gifDataArray addObject:gifData];
    }
    [self creatController];
}
-(void)creatController {
    _image = [[UIImageView alloc]init];
    _image.backgroundColor = [UIColor clearColor];
    if (_isPath) {
        NSString *path = [[NSBundle mainBundle] pathForResource:[_gifArray objectAtIndex:0] ofType:@"gif"];
        NSData *gifData = [NSData dataWithContentsOfFile:path];
        _image.image = [UIImage sd_animatedGIFWithData:gifData];
    } else {
        [_image setImageURLStr:[_gifArray objectAtIndex:0] placeholder:[UIImage imageNamed:@"ali"]];
    }
    _image.layer.cornerRadius = 5;
    _image.layer.masksToBounds = YES;
    [self.view addSubview:_image];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[UIImage imageNamed:@"11_03"] forState:UIControlStateNormal];
    button.tag = 0;
    [self.view addSubview:button];
    
    _save = [UIButton buttonWithType:UIButtonTypeCustom];
    [_save addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_save setBackgroundImage:[UIImage imageNamed:@"11_06"] forState:UIControlStateNormal];
    _save.tag = 1;
    [self.view addSubview:_save];
    
    if ([self findSQl]) {
        [_save setBackgroundImage:[UIImage imageNamed:@"shoucang_03 2"] forState:UIControlStateNormal];
    }
    
    static int index;
    static int row;
    if (kwidth == 320) {
        if (kHeight == 568) {
            index = 9;
            row = 3;
        } else {
            index = 6;
            row = 3;
        }
    } else if (kwidth == 414) {
        index = 12;
        row = 4;
    } else if (kwidth == 375) {
        index = 9;
        row = 3;
    }
    if (_gifArray.count % index != 0) {
        _x = _gifArray.count / index + 1;
    } else {
        _x = _gifArray.count / index;
    }
    _scroll = [[UIScrollView alloc]init];
    _scroll.delegate = self;
    _scroll.showsHorizontalScrollIndicator = NO;
    _scroll.pagingEnabled = YES;
    [self.view addSubview:_scroll];
    
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(_scroll.center.x-50, kHeight-10, 100, 9)];
    _pageControl.numberOfPages = _x;
    _pageControl.currentPage = 0;
    _pageControl.userInteractionEnabled = YES;
    _pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:0.96 green:0.56 blue:0.15 alpha:1];
    _pageControl.pageIndicatorTintColor = [UIColor colorWithRed:0.91 green:0.75 blue:0.45 alpha:1];
    [_pageControl addTarget:self action:@selector(pageClickChangeScroll:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_pageControl];
    
    if (kwidth == 320) {
        if (kHeight == 480) {
            _image.frame = CGRectMake(44, 25 + 64, 125, 125);
            _scroll.frame = CGRectMake(0, 173 + 64, kwidth, 223);
            _scroll.contentSize = CGSizeMake(kwidth * _x, 223);
            _pageControl.center = CGPointMake(_scroll.center.x, kHeight-30);
            button.frame = CGRectMake(200, 110, 87, 32);
            _save.frame = CGRectMake(200, 173, 87, 32);
        } else {
            _image.frame = CGRectMake(10, 84, 164, 164);
            button.frame = CGRectMake(200, 110, 87, 32);
            _save.frame = CGRectMake(200, 173, 87, 32);
            _scroll.frame = CGRectMake(0, kHeight - 310, kwidth, 293);
            _scroll.contentSize = CGSizeMake(kwidth * _x, 293);
            _pageControl.center = CGPointMake(_scroll.center.x, kHeight-10);
        }
    } else if (kwidth == 414) {
        _image.frame = CGRectMake(56, 108, 164, 164);
        button.frame = CGRectMake(262, 67 + 64, 111.5, 42);
        _save.frame = CGRectMake(262, 197, 111.5, 42);
        _scroll.frame = CGRectMake(0, 317, kwidth, 340);
        _scroll.contentSize = CGSizeMake(kwidth * _x, 340);
        _pageControl.center = CGPointMake(_scroll.center.x, kHeight - 60);
    } else if (kwidth == 375) {
        _image.frame = CGRectMake(52.5, 28+64, 147.5, 147.5);
        button.frame = CGRectMake(236, 115.5, 111.5, 42);
        _save.frame = CGRectMake(236, 175, 111.5, 42);
        _scroll.frame = CGRectMake(0, 257.5, kwidth, 391);
        _scroll.contentSize = CGSizeMake(kwidth * _x, 391);
        _pageControl.center = CGPointMake(_scroll.center.x, kHeight - 40);
    }
    int onePageNum;
    int viewTag = 0;
    for (int i = 0; i < _x; i++) {
        if (i == _gifArray.count / index) {
            onePageNum = _gifArray.count % index;
        } else {
            onePageNum = index;
        }
        for (int j = 0; j < onePageNum; j++) {
            UIView *back = [[UIView alloc]init];
            back.tag = viewTag;
            back.backgroundColor = [UIColor whiteColor];
            back.layer.cornerRadius = 5;
            back.layer.masksToBounds = YES;
            back.layer.shadowColor = [UIColor colorWithRed:0.73 green:0.73 blue:0.73 alpha:1].CGColor;
            back.layer.shadowOffset = CGSizeMake(1, 1);
            back.layer.shadowOpacity = 0.5;
            back.layer.shadowRadius = 1.0;
            back.layer.borderColor = [[UIColor colorWithRed:0.96 green:0.78 blue:0.35 alpha:1] CGColor];
            back.layer.borderWidth = 1;
            if (kwidth == 320) {
                if (kHeight == 568) {
                    if (j < row) {
                        back.frame = CGRectMake(20 + i * kwidth + j * (85 + 13), 0, 85, 85);
                    } else if (j < row * 2) {
                        int k = j - row;
                        back.frame = CGRectMake(20 + i * kwidth + k * (85 + 13), 102, 85, 85);
                    } else {
                        int k = j - row * 2;
                        back.frame = CGRectMake(20 + i * kwidth + k * (85 + 13), 102 + 102, 85, 85);
                    }
                } else {
                    if (j < row) {
                        back.frame = CGRectMake(20 + i * kwidth + j * (85 + 15), 0, 85, 85);
                    } else {
                        int k = j - row;
                        back.frame = CGRectMake(20 + i * kwidth + k * (85 + 15), 102, 85, 85);
                    }
                }
                
            } else if (kwidth == 414) {
                if (j < row) {
                    back.frame = CGRectMake(15 + i * kwidth + j * (75 + 23), 0, 90, 90);
                } else if (j < row * 2){
                    int k = j - row;
                    back.frame = CGRectMake(15 + i * kwidth + k * (75 + 23), 105, 90, 90);
                } else {
                    int k = j - row * 2;
                    back.frame = CGRectMake(15 + i * kwidth + k * (75 + 23), 105 + 105, 90, 90);
                }
            } else if (kwidth == 375) {
                if (j < row) {
                    back.frame = CGRectMake(22 + i * kwidth + j * (90 + 29), 0, 91, 91);
                } else if (j < row * 2){
                    int k = j - row;
                    back.frame = CGRectMake(22 + i * kwidth + k * (90 + 29), 120, 91, 91);
                } else {
                    int k = j - row * 2;
                    back.frame = CGRectMake(22 + i * kwidth + k * (90 + 29), 120 * 2, 91, 91);
                }
            }
            [back addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewClick:)]];
            [_scroll addSubview:back];
            
            UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(3, 2, 84, 84)];
            image.layer.cornerRadius = 5;
            image.layer.masksToBounds = YES;
            if (_isPath) {
                [image setImageURL:[NSURL fileURLWithPath:[_gifUrlArray objectAtIndex:viewTag]] placeholder:[UIImage imageNamed:@"ali"]];
//                image.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.gif",[_gifArray objectAtIndex:viewTag]]];
            } else {
                [image setImageURLStr:[_gifArray objectAtIndex:viewTag] placeholder:[UIImage imageNamed:@"ali"]];
            }
            [back addSubview:image];
            
            UILabel *label;
            UIImageView *line;
            if (_hasName) {
                label = [[UILabel alloc]initWithFrame:CGRectMake(0, 70, 90, 20)];
                if (_isPath) {
                    label.text = [_gifArray objectAtIndex:viewTag];
                } else {
                    label.text = [_gifNameArray objectAtIndex:viewTag];
                }
                line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 70, 90, 20)];
                line.backgroundColor = [UIColor whiteColor];
                [back addSubview:line];
                
                label.textColor = [UIColor colorWithRed:0.96 green:0.55 blue:0.15 alpha:1];
                label.font = [UIFont fontWithName:@"MFYaYuan_Noncommercial-Regular" size:12];
                label.textAlignment = NSTextAlignmentCenter;
                [back addSubview:label];
            }
            
            if (kwidth == 320) {
                image.frame = CGRectMake(4, 3.5, 77, 77);
                line.frame = CGRectMake(0, 65, 85, 20);
                label.frame = CGRectMake(0, 65, 85, 20);
                label.font = [UIFont fontWithName:@"MFYaYuan_Noncommercial-Regular" size:11];
            } else if (kwidth == 375) {
                image.frame = CGRectMake(4, 4, 82, 82);
                line.frame = CGRectMake(0, 70, 90, 20);
                label.frame = CGRectMake(0, 70, 90, 20);
                label.font = [UIFont fontWithName:@"MFYaYuan_Noncommercial-Regular" size:12];
            }
            viewTag++;
        }
    }
}
-(void)viewClick:(UITapGestureRecognizer *)tap {
    _gifNum = tap.view.tag;
    if (_isPath) {
        NSString *path = [[NSBundle mainBundle] pathForResource:[_gifArray objectAtIndex:tap.view.tag] ofType:@"gif"];
        NSData *gifData = [NSData dataWithContentsOfFile:path];
        _image.image = [UIImage sd_animatedGIFWithData:gifData];
    } else {
        [_image setImageURLStr:[_gifArray objectAtIndex:_gifNum] placeholder:[UIImage imageNamed:@"ali"]];
    }
}
-(void)pageClickChangeScroll:(UIPageControl *)aPageControl {
    _scroll.contentOffset = CGPointMake(aPageControl.currentPage * kwidth, 0);
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    _pageControl.currentPage = scrollView.contentOffset.x / kwidth;
}
-(void)buttonClick:(UIButton *)sender {
    if (sender.tag == 0) {
        _back = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kwidth, kHeight)];
        _back.backgroundColor = [UIColor colorWithRed:0.98 green:0.96 blue:0.83 alpha:0.55];
        [_back addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeShare)]];
        [self.view addSubview:_back];
        
        _share = [[UIView alloc]init];
        _share.frame = CGRectMake(10, kHeight, kwidth, 230);
        _share.backgroundColor = [UIColor colorWithRed:0.98 green:0.96 blue:0.85 alpha:0.9];
        [self.view addSubview:_share];
        
        UIImageView *fenXiang = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kwidth, 52)];
        fenXiang.backgroundColor = [UIColor colorWithRed:0.74 green:0.65 blue:0.26 alpha:1];
        [_share addSubview:fenXiang];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 52)];
        label.text = @"发送";
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont fontWithName:@"MFYaYuan_Noncommercial-Regular" size:20];
        label.textAlignment = NSTextAlignmentCenter;
        label.center = fenXiang.center;
        [_share addSubview:label];
        
        UIButton *weiChat = [UIButton buttonWithType:UIButtonTypeCustom];
        [weiChat setBackgroundImage:[UIImage imageNamed:@"share1_05"] forState:UIControlStateNormal];
        weiChat.frame = CGRectMake(23, 48, 77, 108);
        weiChat.tag = 0;
        [weiChat addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
        [_share addSubview:weiChat];
        
        UIButton *weiXin = [UIButton buttonWithType:UIButtonTypeCustom];
        [weiXin setBackgroundImage:[UIImage imageNamed:@"share1_08"] forState:UIControlStateNormal];
        weiXin.frame = CGRectMake(_share.frame.size.width - 30 - 97, 48, 77, 108);
        weiXin.tag = 1;
        [weiXin addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
        [_share addSubview:weiXin];
        
        UIButton *qq = [UIButton buttonWithType:UIButtonTypeCustom];
        [qq setBackgroundImage:[UIImage imageNamed:@"share1_10"] forState:UIControlStateNormal];
        qq.frame = CGRectMake(30, 48, 77, 108);
        qq.tag = 2;
        [qq addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
        [_share addSubview:qq];
        
        UIButton *sina = [UIButton buttonWithType:UIButtonTypeCustom];
        [sina setBackgroundImage:[UIImage imageNamed:@"share1_12"] forState:UIControlStateNormal];
        sina.frame = CGRectMake(_share.frame.size.width - 30 - 97, 48, 77, 108);
        sina.tag = 3;
        [sina addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
        [_share addSubview:sina];
        
        if (kwidth == 414) {
            weiChat.frame = CGRectMake(23, 48 + 42, 77, 108);
            weiXin.frame = CGRectMake(120, 48 + 42, 77, 108);
            qq.frame = CGRectMake(213, 48 + 42, 77, 108);
            sina.frame = CGRectMake(314, 48 + 42, 77, 108);
        } else if (kwidth == 320) {
            weiChat.frame = CGRectMake(18, 85, 61, 88);
            weiXin.frame = CGRectMake(94, 85, 61, 88);
            qq.frame = CGRectMake(168, 85, 61, 88);
            sina.frame = CGRectMake(242, 85, 61, 88);
        } else if (kwidth == 375) {
            weiChat.frame = CGRectMake(21, 90, 71, 99);
            weiXin.frame = CGRectMake(109, 90, 71, 99);
            qq.frame = CGRectMake(198, 90, 71, 99);
            sina.frame = CGRectMake(284, 90, 71, 99);
        }
        
        [UIView animateWithDuration:0.5 animations:^{
            _share.frame = CGRectMake(0, kHeight - 220, kwidth, 230);
        } completion:^(BOOL finished) {
            
        }];
    } else {
        if ([self findSQl]) {
            [self alertViewShow:@"已收藏过了"];
        } else {
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
            NSLog(@"%@",paths);
            BOOL result = NO;
            
            myData *data = [[myData alloc]init];
            [data initData];
            if (_hasName) {
                if (_isPath) {
                    [data insertData:_gifName biaoQingBao:@"" gifImage:[_gifArray objectAtIndex:0] tableName:@"biaoQingBao" hasName:1];
                } else {
                    [data insertData:_gifName biaoQingBao:@"" gifImage:[_gifNameArray objectAtIndex:0] tableName:@"biaoQingBao"hasName:1];
                }
            } else {
                if (_isPath) {
                    [data insertData:_gifName biaoQingBao:@"" gifImage:[_gifArray objectAtIndex:0] tableName:@"biaoQingBao" hasName:0];
                } else {
                    [data insertData:_gifName biaoQingBao:@"" gifImage:[NSString stringWithFormat:@"%@0",_gifName] tableName:@"biaoQingBao"hasName:0];
                }
            }
            
            for (int i = 0; i < _gifDataArray.count; i++) {
                if (_hasName) {
                    if (_isPath) {
                        NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.gif",[_gifArray objectAtIndex:i]]];   // 保存文件的名称
                        NSError *error;
                        result = [[_gifDataArray objectAtIndex:i] writeToFile:filePath options:NSDataWritingAtomic error:&error];
                        [data insertData:[_gifArray objectAtIndex:i] biaoQingBao:_gifName gifImage:[_gifArray objectAtIndex:i] tableName:@"biaoQing"hasName:0];
                    } else {
                        NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.gif",[_gifNameArray objectAtIndex:i]]];   // 保存文件的名称
                        NSError *error;
                        result = [[_gifDataArray objectAtIndex:i] writeToFile:filePath options:NSDataWritingAtomic error:&error];
                        [data insertData:[_gifNameArray objectAtIndex:i] biaoQingBao:_gifName gifImage:[_gifNameArray objectAtIndex:i] tableName:@"biaoQing"hasName:0];
                    }
                } else {
                    if (_isPath) {
                        NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.gif",[_gifArray objectAtIndex:i]]];   // 保存文件的名称
                        NSError *error;
                        result = [[_gifDataArray objectAtIndex:i] writeToFile:filePath options:NSDataWritingAtomic error:&error];
                        [data insertData:[_gifArray objectAtIndex:i] biaoQingBao:_gifName gifImage:[_gifArray objectAtIndex:i] tableName:@"biaoQing"hasName:0];
                    } else {
                        NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%d.gif",_gifName,i]];   // 保存文件的名称
                        NSError *error;
                        result = [[_gifDataArray objectAtIndex:i] writeToFile:filePath options:NSDataWritingAtomic error:&error];
                        [data insertData:[NSString stringWithFormat:@"%@%d",_gifName,i] biaoQingBao:_gifName gifImage:[NSString stringWithFormat:@"%@%d",_gifName,i] tableName:@"biaoQing"hasName:0];
                    }
                }
            }
            if (result) {
                [self alertViewShow:@"收藏成功!"];
            } else {
                [self alertViewShow:@"收藏失败了..."];
            }
            [_save setBackgroundImage:[UIImage imageNamed:@"shoucang_03 2"] forState:UIControlStateNormal];
        }
    }
}
-(void)share:(UIButton *)button {
    id<ISSCAttachment> image;
    if (_isPath) {
        image = [ShareSDK imageWithPath:[_gifUrlArray objectAtIndex:_gifNum]];
    } else {
        image = [ShareSDK imageWithUrl:[_gifArray objectAtIndex:_gifNum]];
    }
    if (button.tag == 0) {
        //构造分享内容
        id<ISSContent> publishContent = [ShareSDK content:@"这是在分享图片,这个不显示的"
                                           defaultContent:@"测试一下分享Gif图片"
                                                    image:image
                                                    title:@"ShareSDK"
                                                      url:@"http://www.mob.com"
                                              description:@"这是一条测试信息"
                                         //注意此分享Gif的类型只适用于微信
                                                mediaType:SSPublishContentMediaTypeGif];
        //定制微信好友信息
        [publishContent addWeixinSessionUnitWithType:[NSNumber numberWithInteger:
                                                      SSPublishContentMediaTypeGif]
                                             content:@"这是在分享图片,这个不显示的"
                                               title: @"Hello 微信好友!"
                                                 url:@"http://www.mob.com"
                                               image:image
                                        musicFileUrl:nil
                                             extInfo:nil
                                            fileData:nil
                                        emoticonData:[_gifDataArray objectAtIndex:_gifNum]];
        [ShareSDK shareContent:publishContent
                          type:ShareTypeWeixiSession
                   authOptions:nil
                  shareOptions:nil
                 statusBarTips:YES
                        result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                            if (state == SSPublishContentStateSuccess){
                                NSLog(NSLocalizedString(@"TEXT_SHARE_SUC", @"发表成功"));
                            } else if (state == SSPublishContentStateFail) {
                                NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"发布失败!error code == %d, error code == %@"), [error errorCode], [error errorDescription]);
                            }
                        }];
        [self removeShare];
    } else if (button.tag == 1) {
        //发送内容给微信
        id<ISSContent> content = [ShareSDK content:nil
                                    defaultContent:nil
                                             image:image
                                             title:nil
                                               url:nil
                                       description:nil
                                         mediaType:SSPublishContentMediaTypeImage];
        [ShareSDK shareContent:content
                          type:ShareTypeWeixiTimeline
                   authOptions:nil
                 statusBarTips:YES
                        result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                            if (state == SSPublishContentStateSuccess) {
                                NSLog(@"success");
                                [self removeShare];
                            } else if (state == SSPublishContentStateFail) {
                                if ([error errorCode] == -22003) {
                                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"TEXT_TIPS", @"提示") message:[error errorDescription] delegate:nil cancelButtonTitle:NSLocalizedString(@"TEXT_KNOW", @"知道了") otherButtonTitles:nil];
                                    [alertView show];
                                }
                            }
                        }];
        [self removeShare];
    } else if (button.tag == 2) {
        id<ISSContent> publishContent = [ShareSDK content:@"这是在分享图片,这个不显示的"
                                           defaultContent:@""
                                                    image:image
                                                    title:@"ShareSDK"
                                                      url:@"http://www.mob.com"
                                              description:@"这是一条测试信息"
                                         //注意QQ好友分享gif图片要使用image类型的
                                                mediaType:SSPublishContentMediaTypeImage];
        [ShareSDK shareContent:publishContent
                          type:ShareTypeQQ
                   authOptions:nil
                  shareOptions:nil
                 statusBarTips:YES
                        result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                            if (state == SSPublishContentStateSuccess){
                                NSLog(NSLocalizedString(@"TEXT_SHARE_SUC", @"发表成功"));
                            } else if (state == SSPublishContentStateFail) {
                                NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"发布失败!error code == %d, error code == %@"), [error errorCode], [error errorDescription]);
                            }
                        }];
        [self removeShare];

    } else if (button.tag == 3) {
        WeiBoViewController *weibo = [[WeiBoViewController alloc]init];
        weibo.imageData = [_gifDataArray objectAtIndex:_gifNum];
        if (_isPath) {
            weibo.image = [_gifUrlArray objectAtIndex:_gifNum];
        } else {
            weibo.image = [_gifArray objectAtIndex:_gifNum];
        }
        weibo.isPath = _isPath;
        self.navigationItem.title = @"返回";
        [self.navigationController pushViewController:weibo animated:YES];
        [self removeShare];
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    [self removeShare];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
